#!/bin/sh

# 运行环境CentOS 7.x

name="datacube_test"
pass="y93exp3IsHBFUrFA"
hosts=(
  "173.18.162.30 datacube-30.test.local"
  "173.18.162.31 datacube-31.test.local"
  "173.18.162.32 datacube-32.test.local"
  "173.18.162.33 datacube-33.test.local"
  "173.18.162.34 datacube-34.test.local"
  "173.18.162.35 datacube-35.test.local"
  "173.18.162.36 datacube-36.test.local"
  "173.18.162.37 datacube-37.test.local"
)

echo "-------------步骤1: 创建管理用户,请稍等-------------"
echo "INFO: you are setting username : ${name}"
echo "INFO: you are setting password : $pass for ${name}"

sudo useradd $name -d /data/$name
if [ $? -eq 0 ];then
   echo "INFO: user ${name} is created successfully!!!"
else
   echo "ERROR: user ${name} is created failly!!!"
   exit 1
fi
#sudo passwd $name会要求填入密码，下面将$pass作为密码传入
echo $pass | sudo passwd $name --stdin &>/dev/null
if [ $? -eq 0 ];then
   echo "INFO: ${name}'s password is set successfully"
else
   echo "ERROR: ${name}'s password is set failly!!!"
fi



echo "-------------步骤2: 更换为Yum、epel源,请稍等-------------"
#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
#yum clean all &>/dev/null
#yum makecache &>/dev/null
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat >> /etc/yum.repos.d/kylin.repo << EOF
[http_iso]
name=iso_105
baseurl=http://173.18.162.30/iso
gpgcheck=0
enabled=1
EOF
yum clean all
yum makecache

echo "-------------步骤3: 下载常用工具，请稍后-------------"
yum -y install gcc gcc-c++ net-tools make autoconf sysstat bash-completion wget vim telnet libcurl lrzsz dos2unix nohup unzip openssl-devel rsync lsof lvm2&>/dev/null
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

echo "-------------步骤4: 修改字符集-------------"
sed -i 's/LANG="en_US.UTF-8"/LANG="zh_CN.UTF-8"/' /etc/locale.conf

echo "-------------步骤5: 同步时间,请稍等-------------"
if [ -f /usr/sbin/ntpdate ];then
echo ""
else
yum -y install ntp &>/dev/null
fi
/usr/sbin/ntpdate cn.pool.ntp.org &>/dev/null
echo "0 * * * *  /usr/sbin/ntpdate time1.aliyun.com >> /tmp/autontpdate 2>&1" >> /var/spool/cron/root
systemctl restart crond.service
0 * * * *  /usr/sbin/ntpdate time1.aliyun.com >> /tmp/autontpdate 2>&1

echo "-------------步骤6: 设置时区,请稍等-------------"
timedatectl set-timezone Asia/Shanghai

echo "-------------步骤7: 内核参数优化,请稍等-------------"
echo "ulimit -SHn 102400" >> /etc/rc.local
echo "vm.max_map_count=65535" >>/etc/sysctl.conf
echo "net.core.somaxconn=65535" >>/etc/sysctl.conf
echo "vm.overcommit_memory=1" >>/etc/sysctl.conf
echo "vm.swappiness=10">> /etc/sysctl.conf
sysctl -p
# CentOS7 用户进程数
sed -i 's#4096#65535#g' /etc/security/limits.d/20-nproc.conf

echo "-------------步骤8: 设置最大打开文件描述符数,请稍等-------------"
cat >> /etc/security/limits.conf << EOF
* soft nofile 655350
* hard nofile 655350
* soft nproc 655350
* hard nproc 655350
* soft stack unlimited
* hard memlock unlimited
${name} soft nofile 655350
${name} hard nofile 655350
${name} soft memlock unlimited
${name} hard memlock unlimited
EOF

echo "-------------步骤9: 禁止定时任务向发送邮件,请稍等-------------"
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab

echo "-------------步骤10: 禁用SWAP使用,请稍等-------------"
sed -i '/swap/s/^/#/' /etc/fstab
swapoff -a

echo "-------------步骤11: 禁用selinux,请稍等-------------"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

echo "-------------步骤12: 关闭防火墙,请稍等-------------"
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
    systemctl stop firewalld
    systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
    service iptables stop
    chkconfig iptables off
fi

echo "-------------步骤13: 禁用透明大页,请稍等-------------"
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.d/rc.local
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

echo "-------------步骤14: 修改hostname, hosts-------------"
sed -i '1,2 s/^/# /' /etc/hosts
ip=$(ip addr show | grep 'inet ' | grep -v '127\.0\.0\.1' | head -n1 | awk '{print $2}' | cut -f1 -d'/')
hostnamectl set-hostname "datacube-${ip##*.}.test.local"

for h in "${hosts[@]}"
do
  echo "$h" | sudo tee -a /etc/hosts > /dev/null
done
cat /etc/hosts

echo "-------------步骤15: 重启系统，请输入reboot命令-------------"
# reboot